package com.medistat.controllers;


import java.io.Serializable;
import java.util.Calendar;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.bean.SessionScoped;

import org.primefaces.model.chart.PieChartModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.medistat.dialog.services.DialogService;
import com.medistat.helpers.UrlConsts;
import com.medistat.model.enums.Months;

@Controller("statisticsController")
@SessionScoped
public class StatisticsController extends AbstractController implements Serializable{

	private static final long serialVersionUID = 5649185596054577007L;
	private DialogService dialogService;

	private PieChartModel pieChartModel;
	private Months selectedMonth;
	private Integer selectedYear;
	
	@Autowired
	public void setDialogService(DialogService dialogService) {
		this.dialogService = dialogService;
	}
	
	@PostConstruct
	public void initialize() {
	}
	
	public PieChartModel getPieChartModel() {
		if(pieChartModel==null) {
			Map<String, Integer> map = null;
			try {
				map = dialogService.getAllDutiesForAllDoctorsInYear(getSelectedYear());
			} catch (Exception e) {
				addError(e.getMessage());
			}
			pieChartModel = new PieChartModel();
			for(Map.Entry<String, Integer> entry : map.entrySet()) {
				pieChartModel.set(entry.getKey(), entry.getValue());	
			}
		}
		return pieChartModel;
	}
	
	public Months getSelectedMonth() {
		return selectedMonth;
	}

	public void setSelectedMonth(Months selectedMonth) {
		this.selectedMonth = selectedMonth;
	}

	public Integer getSelectedYear() {
		return selectedYear;
	}

	public void setSelectedYear(Integer selectedYear) {
		this.selectedYear = selectedYear;
	}
	
	public void getNewValues() {
		Map<String, Integer> map = null;
		try {
			if(selectedMonth==null) {
				Calendar calendar = Calendar.getInstance();
				map = dialogService.getAllDutiesForAllDoctorsInYear(getSelectedYear());
			} else {
				map = dialogService.getAllDutiesForAllDoctorsInMonth(getSelectedYear(), selectedMonth.getValue());
			}
		} catch (Exception e) {
			addError(e.getMessage());
		}
		pieChartModel = new PieChartModel();
		for(Map.Entry<String, Integer> entry : map.entrySet()) {
			pieChartModel.set(entry.getKey(), entry.getValue());	
		}
	}

	@Override
	public String getPageName() {
		return UrlConsts.STATISTICS_PAGE;
	}
}
